//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft
// premium shared source license agreement under which you licensed
// this source code. If you did not accept the terms of the license
// agreement, you are not authorized to use this source code.
// For the terms of the license, please see the license agreement
// signed by you and Microsoft.
// THE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES OR INDEMNITIES.
//

#pragma once

#include <stdio.h>
#include <tchar.h>
#include <Windows.h>
#include <wincrypt.h>
#include <atlenc.h>
#include <comutil.h>
#include <MsXml6.h>

#define MAX_PASSWORD 260
#define CHECKFAIL(__val) CHECK(false,__val)  
#define CHECKHRRET(__ex) if( FAILED(hr=__ex) ) goto Exit;  
#define CHECKHR(__ex) if( FAILED(hr=__ex) ) goto Exit;  
#define CHECK(__ex, __val) if( (__ex) == false ) { hr = __val; goto Exit; }  
#define CHECKPARAM(__ex) CHECK(__ex, E_INVALIDARG)  
#define CHECKMEM(__ex) CHECK( (__ex)!=NULL, E_OUTOFMEMORY)  
#define COUNTOF( a ) (sizeof(a)/sizeof(a[0]))  
#define IGNORERET(x) (x)

#define CHECKWIN32(__exp)\
    if((__exp)==FALSE) { \
    hr = HRESULT_FROM_WIN32( ::GetLastError() ); \
    goto Exit; \
    }

//
//  Acquires an crypto context
//
HRESULT AcquireContext(
    _In_  DWORD nAlgorithm,
    _Out_ HCRYPTPROV& hProv
    );

//
//  Generates an encryption key by hashing a buffer (password).
//
HRESULT CreateHashKey(
    _In_  HCRYPTPROV hProv,        // the crypto provider
    _In_  PBYTE   pbKey,          // the encryption key
    _In_  DWORD   nKeySize,       // the size of the encryption key
    _In_  DWORD   nHashAlgorithm, // the algorithm use to hass the key
    _In_  DWORD   nEncryptAlgorithm, // the encryption algorithm
    _Out_ HCRYPTKEY&  hKey        // The encryption key
    );

//
//  Encrypts a buffer using the specified algorithm with the specified key
//
HRESULT EncryptBufferNoHeader (
    _In_  PBYTE   pbKey,          // the encryption key
    _In_  DWORD   nKeySize,       // the size of the encryption key
    _In_  PBYTE   pbInput,        // the input that is to be encrypted
    _In_  DWORD   nInputSize,     // the lsize of the input
    _Out_ PBYTE  &pbEncrypted,    // the output (to be freed by the caller)
    _Out_ DWORD  &nEncryptedSize, // the size of the output
    _In_  DWORD   nAlgorithm
    );

//
//  Encrypts a buffer using the specified algorithm with the specified key
//  and the base64 encodes the result.
//
HRESULT EncryptAndEncodeBuffer (
    _In_  PBYTE   pbKey,          // the encryption key
    _In_  DWORD   nKeySize,       // the size of the encryption key
    _In_  PBYTE   pbInput,        // the input that is to be encrypted
    _In_  DWORD   nInputSize,     // the lsize of the input
    _Out_ PBYTE  &pbOutput,       // the output (to be freed by the caller)
    _Out_ DWORD  &nOutputSize,    // the size of the output
    _In_  DWORD   nAlgorithm
    );